package com.sk.sourcecircle.dialog;

import android.app.DialogFragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.sk.sourcecircle.R;
import com.sk.sourcecircle.WBShareActivity;
import com.sk.sourcecircle.activity.main.AppFriendShareActivity;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.utils.ImageUtil;
import com.sk.sourcecircle.utils.MyUtils;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by sk on 2017/11/19.
 */

public class ShareDialogFragment extends DialogFragment {


    @BindView(R.id.wx_ll)
    FrameLayout wxLl;
    @BindView(R.id.circle_ll)
    FrameLayout circleLl;
    @BindView(R.id.weibo_ll)
    FrameLayout weiboLl;
    @BindView(R.id.qq_ll)
    FrameLayout qqLl;
    @BindView(R.id.cancel)
    ImageView cancel;
    Unbinder unbinder;

    private IWXAPI wxApi;
    private Tencent mTencent;

    private IUiListener qqListener;
    private String imageUrl;
    private String title;
    private String summary;
    private String targetUrl;
    private Bitmap thumb;
    private String activityShare;

    public String getActivityShare() {
        return activityShare;
    }

    public void setActivityShare(String activityShare) {
        this.activityShare = activityShare;
    }

    public IUiListener getQqListener() {
        return qqListener;
    }

    public void setQqListener(IUiListener qqListener) {
        this.qqListener = qqListener;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getTargetUrl() {
        return targetUrl;
    }

    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }

    public Bitmap getThumb() {
        return thumb;
    }

    public void setThumb(Bitmap thumb) {
        this.thumb = thumb;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int style = android.support.v4.app.DialogFragment.STYLE_NO_TITLE, theme = 0;
        setStyle(style, theme);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.dialog_share, container, false);
        unbinder = ButterKnife.bind(this, view);
        mTencent = MyApplication.getInstance().getTencent();
        return view;
    }

    public void onStart() {
        super.onStart();
        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes(layoutParams);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.wx_ll, R.id.circle_ll, R.id.weibo_ll, R.id.qq_ll, R.id.cancel, R.id.app_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wx_ll:
                dismiss();
                wechatShare(0);
                break;
            case R.id.circle_ll:
                dismiss();
                wechatShare(1);
                break;
            case R.id.weibo_ll:
                dismiss();
                weiboShare();
                break;
            case R.id.qq_ll:
                dismiss();
                qqShare(0);
                break;
            case R.id.cancel:
                dismiss();
                break;
            case R.id.app_ll:
                dismiss();
                appShare();
                break;
        }
    }

    private void appShare() {
        Intent intent = new Intent(getActivity(), AppFriendShareActivity.class);
        intent.putExtra("title", title);
        if (activityShare != null && activityShare.length() > 0) {
            intent.putExtra("targetUrl", activityShare);
        } else {
            intent.putExtra("targetUrl", targetUrl);
        }
        intent.putExtra("imageUrl", imageUrl);
        intent.putExtra("summary", summary);
        startActivity(intent);
    }

    private void weiboShare() {
        Intent intent = new Intent(getActivity(), WBShareActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("targetUrl", targetUrl);
        intent.putExtra("imageUrl", imageUrl);
        intent.putExtra("summary", summary);
        startActivity(intent);
    }


    /**
     * 0是好友
     * 1是朋友圈
     *
     * @param flag
     */
    private void wechatShare(final int flag) {
        wxApi = MyApplication.getInstance().getIwxapi();

        if (!wxApi.isWXAppInstalled()) {
            Toast.makeText(getActivity(), "您尚未安装微信客户端！", Toast.LENGTH_SHORT).show();
            return;
        }
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = targetUrl;
        final WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = summary;

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Bitmap thumb2;
                    if (imageUrl != null && imageUrl.length() > 0) {
                        Bitmap bitmap = MyUtils.getBitmap(imageUrl);
                        thumb2 = ImageUtil.imageZoom(bitmap);
                    } else {
                        thumb2 = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
                    }


                    msg.setThumbImage(thumb2);
                    SendMessageToWX.Req req = new SendMessageToWX.Req();
                    req.transaction = String.valueOf(System.currentTimeMillis());
                    req.message = msg;
//        req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
                    req.scene = flag;
                    wxApi.sendReq(req);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }


    /**
     * 0是好友
     * 1是空间
     *
     * @param flag
     */
    private void qqShare(int flag) {
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary);
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, targetUrl);
        String imageUrl2 = imageUrl;//默认图片
        if (imageUrl2 != null && imageUrl2.length() > 0) {
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageUrl2);
        } else {
            Bitmap thumb2 = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
            params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, MyUtils.saveBitmap(thumb2));
        }


        if (flag == 1) {
            params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        }

        mTencent.shareToQQ(getActivity(), params, qqListener);
    }

}
